package com.suxin.netty.http;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @Classname NettyHttpServer
 * @Description [ TODO ]
 * @Author Tang
 * @Date 2021/1/3 20:13
 * @Created by ASUS
 */
public class NettyHttpServer {

    public static void main(String[] args) {

        NioEventLoopGroup bossGroup = new NioEventLoopGroup();
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();

        ServerBootstrap serverBootstrap = new ServerBootstrap();

        try {
            serverBootstrap
                    .group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG, 128)
                    .childOption(ChannelOption.SO_KEEPALIVE, true)
                    // 自定义 Handler
                    .childHandler(new HttpChannelInitializer());

            System.out.println("...服务器 is ready ...");

            // sync 异步变同步
            ChannelFuture future = serverBootstrap.bind(9000).sync();

            System.out.println(Thread.currentThread().getName() + "------------");
            // 产生阻塞 sync()
            future.channel().closeFuture().sync();

            System.out.println(Thread.currentThread().getName() + "------------");

        } catch (Exception e) {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }

    }

}